package handler;

import com.liaojp.cmsweb.Result.AjaxResult;
import com.liaojp.cmsweb.exception.ErrorPasswordException;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理用户名不存在异常
     * @param e 错误
     * @return 响应
     */
    @ExceptionHandler(UsernameNotFoundException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public AjaxResult handleUsernameNotFoundException(UsernameNotFoundException e) {
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 处理密码错误异常
     * @param e 错误
     * @return 响应
     */
    @ExceptionHandler(ErrorPasswordException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public AjaxResult handleErrorPasswordException(ErrorPasswordException e) {
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 处理其他异常
     * @param e 错误
     * @return 响应
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public AjaxResult handleException(Exception e) {
        return AjaxResult.error("系统内部未知错误" + e.getMessage());
    }
}
